package com.example.controller;

import com.example.utils.DButil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet(name = "addguanzhu",urlPatterns = "/addguanzhu")
public class AddFriendServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection con = DButil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement("INSERT INTO friend (USERID, FRIENDID,GUANZHU) VALUES (?,?,?)");
            ps.setString(1, req.getParameter("userid"));
            ps.setString(2, req.getParameter("guanzhuid"));
            ps.setInt(3, 1);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DButil.closeResources(con,ps);
        }
    }
}
